/* ***************************************************************************
 **  interrupts.c
 **
 **  Project      : ClockMatrix_v0
 **  Target       : PIC18F14K50
 **  Description  : Project used to develop clock firmware.  Tested methods and
 **                 determined functionality (needed, wanted, and the possible)
 **
 **  Compiler     : HI-TECH C  Version 9.80
 **  IDE          : MPLABX v1.80
 **  Created      : 5/8/2013
 **  Branched     : 6/27/2013 -- started v2
 **
 **  R Hanlen <implod@gmail.com>
 ** ***************************************************************************/

/*****************************************************************************
 ** Files to Include
 *****************************************************************************/

#include <htc.h>            /* HiTech General Includes */
#include <stdint.h>         /* For uint8_t definition */
#include <stdbool.h>        /* For true/false definition */

#include "global.h"
#include "shift_register.h"
/*****************************************************************************
 ** Variables
 *****************************************************************************/


/*****************************************************************************
 ** Interrupt Functions
 *****************************************************************************/

/* High-priority service */
void interrupt high_isr(void){

    if ((TMR3IE) && (TMR3IF)){
        milliseconds++;
        WRITETIMER3(53536);
        TMR3IF = CLEAR; // Clear the interrupt flag
    }
   else if ((INT0IE) && (INT0IF)){

       currentState = 0;
       scrollFlag = 1;
      
      INT0IF = CLEAR;
   }
   

}

/* Low-priority interrupt routine */
void low_priority interrupt low_isr(void){

    if ((TMR0IE) && (TMR0IF)) {

        NextRow();

        display.presentRow++;
        if (display.presentRow == 8) display.presentRow = 0;

        TMR0L = 50; // Reset the timer0 counter
        TMR0IF = 0; // Clear the timer0 interrupt flag
    }
     else if ((INT1IE) && (INT1IF)){

        currentState = 1;
        
         scrollFlag = 1;
        INT1IF = CLEAR;
    }
    else if ((INT2IE) && (INT2IF)){

        currentState = 3;
        
         scrollFlag = 1;
        INT2IF = CLEAR;
    }
}
